package com.shixj.easy.spider.api.sync;

import com.shixj.easy.spider.constant.DomainContants;
import com.shixj.easy.spider.executor.FetchExecutor;
import com.shixj.easy.spider.model.Catalog;
import com.shixj.easy.spider.model.Chapter;
import com.shixj.easy.spider.model.RegexConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by shixj on 2018/1/16.
 */
@RestController
public class DefaultReadController {
    @Autowired
    @Qualifier("syncDefaultFetchExecutor")
    private FetchExecutor executor;

    @GetMapping("book/{domain}/{novelId}/{chapterId}")
    Chapter read(@PathVariable String domain, @PathVariable String novelId, @PathVariable String chapterId) {
        String baseUrl = DomainContants.getDomain(domain);
        RegexConfig config = RegexConfig.build().
                setBaseUrl(baseUrl)
                .setChapterPageRegex(baseUrl + novelId + "/\\d+.html$").
                        setInitUrl(DomainContants.getDomain(domain) + novelId + "/" + chapterId+".html");
        executor.init(config);
        return executor.fetchChapter();
    }

    @GetMapping("book/{domain}/{novelId}")
    Catalog read(@PathVariable String domain, @PathVariable String novelId) {
        String baseUrl = DomainContants.getDomain(domain);
        RegexConfig config = RegexConfig.build().
                setBaseUrl(baseUrl).
                setNovelPageRegex(baseUrl + novelId + "/?").
                setInitUrl(baseUrl + novelId);
        executor.init(config);
        return executor.fetchNovel();
    }
}
